﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Text;
using Store.WP7.Framework.Helpers;

namespace Store.WP7.Framework.Configuration
{
    public class ConfigurationManager
    {
        #region Constants

        private const string CONFIG_CanSendAnonymousReport = "CanSendAnonymousReport";
        private const string CONFIG_CanUsePushData = "CanUsePushData";
        private const string CONFIG_CanUseBackgroundAgent = "CanUseBackgroundAgent";

        #endregion


        #region Singleton

        /// <summary>
        /// Configuration manager unique instance
        /// </summary>
        private static volatile ConfigurationManager instance;

        /// <summary>
        /// Sync object
        /// </summary>
        private static object syncRoot = new Object();

        #region Instance Property

        /// <summary>
        /// Configuration manager instance
        /// </summary>
        public static ConfigurationManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new ConfigurationManager();
                    }
                }

                return instance;
            }
        }

        #endregion

        #endregion

        private bool canSendAnonymousReport;
        public bool CanSendAnonymousReport
        {
            get { return canSendAnonymousReport; }
            set
            {
                if (canSendAnonymousReport != value)
                {
                    canSendAnonymousReport = value;

                    CanSendAnonymousReportChanged();
                }
            }
        }

        private bool canUsePushData;
        public bool CanUsePushData
        {
            get { return canUsePushData; }
            set
            {
                if (canUsePushData != value)
                {
                    canUsePushData = value;

                    CanUsePushDataChanged();
                }
            }
        }

        private bool canUseBackgroundAgent;
        public bool CanUseBackgroundAgent
        {
            get { return canUseBackgroundAgent; }
            set
            {
                if (canUseBackgroundAgent != value)
                {
                    canUseBackgroundAgent = value;

                    CanUseBackgroundAgentChanged();
                }
            }
        }

       
        private ConfigurationManager()
        {
            Initialize();
        }

        /// <summary>
        /// Initialize default values or, if available, user ones
        /// </summary>
        void Initialize()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(CONFIG_CanSendAnonymousReport))
                CanSendAnonymousReport = (bool)IsolatedStorageSettings.ApplicationSettings[CONFIG_CanSendAnonymousReport];
            else
                CanSendAnonymousReport = false; // default value

            if (IsolatedStorageSettings.ApplicationSettings.Contains(CONFIG_CanUsePushData))
                CanUsePushData = (bool)IsolatedStorageSettings.ApplicationSettings[CONFIG_CanUsePushData];
            else
                CanUsePushData = false; // default value

            if (IsolatedStorageSettings.ApplicationSettings.Contains(CONFIG_CanUseBackgroundAgent))
                CanUseBackgroundAgent = (bool)IsolatedStorageSettings.ApplicationSettings[CONFIG_CanUseBackgroundAgent];
            else
                CanUseBackgroundAgent = false; // default value
        }

        private void CanSendAnonymousReportChanged()
        {
            IsolatedStorageSettings.ApplicationSettings[CONFIG_CanSendAnonymousReport] = CanSendAnonymousReport;
        }

        private void CanUsePushDataChanged()
        {
            IsolatedStorageSettings.ApplicationSettings[CONFIG_CanUsePushData] = CanUsePushData;
        }

        private void CanUseBackgroundAgentChanged()
        {
            if (CanUseBackgroundAgent)
                BackgroundAgentHelper.TryEnableBackgroundAgent();

            IsolatedStorageSettings.ApplicationSettings[CONFIG_CanUseBackgroundAgent] = CanUseBackgroundAgent;
        }

        public byte[] GetSettings()
        {
            var settings = new Dictionary<string, bool>();
            settings.Add(CONFIG_CanSendAnonymousReport, CanSendAnonymousReport);
            settings.Add(CONFIG_CanUsePushData, CanUsePushData);
            settings.Add(CONFIG_CanUseBackgroundAgent, CanUseBackgroundAgent);

            var serialized = DataContractSerializerHelper.Serialize(settings);

            return Encoding.Unicode.GetBytes(serialized);
        }
    }
}
